package UILayer;

import java.awt.BorderLayout;

import CtrLayer.StockCtr;
import ModelLayer.CurrentAccount;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;
import javax.swing.table.DefaultTableCellRenderer;

public class ProductUI extends JInternalFrame
{
	private JTextField text_field;
	private StockCtr stock_ctr;
	private JTable product_table;
	private int btn_pressed = 1;
	private JButton complete_btn;
	private String[] column_names = { "Art. number", "Name", "Quantity",
			"Category", "Amount qualified for discount", "Location", "Price" };

	public ProductUI()
	{

		super("Products", true, true, true, true);

		this.addInternalFrameListener(new InternalFrameAdapter()
		{
			public void internalFrameActivated(InternalFrameEvent e)
			{
				refresh();
			}
		});
		stock_ctr = new StockCtr();

		fill_frame();

	}

	private void refresh()
	{
		Object[][] filling;
		if (btn_pressed == 1)
		{
			filling = stock_ctr.display_all_products();
			complete_btn.setVisible(false);
		}
		else if (btn_pressed == 2)
		{
			filling = stock_ctr.display_products_that_need_restocking();
			complete_btn.setVisible(false);
		}
		else
		{
			filling = stock_ctr.display_unfinished_products();
			if(!(CurrentAccount.give_rank().equals("employee")))
			{
				complete_btn.setVisible(true);
			}
		}
		product_table.setModel(new MyTableModel(filling, column_names));
		product_table.getColumnModel().getColumn(4).setMinWidth(180);
	}


	private void make_table()
	{

		final Object[][] filling = stock_ctr.display_all_products();

		product_table = new JTable();

		product_table.setFillsViewportHeight(true);
		product_table.setModel(new MyTableModel(filling, column_names));

		product_table
				.setPreferredScrollableViewportSize(new Dimension(500, 70));
		product_table.setAutoCreateRowSorter(true);
		product_table.setRowSelectionAllowed(true);
		product_table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		product_table.getColumnModel().getColumn(4).setMinWidth(180);
		product_table.getTableHeader().setReorderingAllowed(false);
		product_table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

		product_table.addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent e)
			{
				if (e.getClickCount() == 2)
				{
					details();
				}
			}
		});

		DefaultTableCellRenderer right_renderer = new DefaultTableCellRenderer();
		right_renderer.setHorizontalAlignment(JLabel.RIGHT);
		product_table.setDefaultRenderer(String.class, right_renderer);

		JScrollPane scroll_pane = new JScrollPane(product_table);
		scroll_pane.setBorder(new EmptyBorder(5, 10, 5, 10));
		getContentPane().add(scroll_pane, BorderLayout.CENTER);
	}

	private void make_radio_buttons(JPanel panel)
	{
		JPanel panel_2 = new JPanel();
		panel.add(panel_2, BorderLayout.EAST);
		panel_2.setLayout(new BoxLayout(panel_2, BoxLayout.Y_AXIS));

		JRadioButton all_prod = new JRadioButton("All products");
		all_prod.setSelected(true);
		all_prod.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				btn_pressed = 1;
				refresh();
			}
		});
		panel_2.add(all_prod);

		JRadioButton res_prod = new JRadioButton("Restock products");
		res_prod.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				btn_pressed = 2;
				refresh();
			}
		});
		panel_2.add(res_prod);

		JRadioButton inc_prod = new JRadioButton("Incomplete products");
		inc_prod.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				btn_pressed = 3;
				refresh();
			}
		});
		panel_2.add(inc_prod);

		ButtonGroup bg = new ButtonGroup();
		bg.add(all_prod);
		bg.add(res_prod);
		bg.add(inc_prod);
	}

	private void make_ctr_buttons()
	{
		JPanel ctr_btn_panel = new JPanel();
		ctr_btn_panel.setBorder(new EmptyBorder(3, 3, 0, 3));

		getContentPane().add(ctr_btn_panel, BorderLayout.SOUTH);
		ctr_btn_panel.setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		ctr_btn_panel.add(panel, BorderLayout.WEST);
		
		JButton detail_btn = new JButton("Details");
		detail_btn.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				details();
			}
		});
		panel.add(detail_btn);
		
		JPanel panel_1 = new JPanel();
		ctr_btn_panel.add(panel_1, BorderLayout.EAST);

		JButton btn_new = new JButton("New");
		btn_new.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				add_product();
			}
		});
		panel_1.add(btn_new);

		JButton btn_edit = new JButton("Edit");
		btn_edit.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				edit();
			}
		});
		
		complete_btn = new JButton("Complete");
		complete_btn.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				complete();
			}
		});
		panel_1.add(complete_btn);
		panel_1.add(btn_edit);

		JButton btn_del = new JButton("Delete");
		btn_del.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				delete();
			}
		});
		panel_1.add(btn_del);
	}

	private void make_search(JPanel panel)
	{
		JPanel search_panel = new JPanel();
		panel.add(search_panel, BorderLayout.WEST);
		search_panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		JLabel lblNewLabel = new JLabel("Name/ID");
		search_panel.add(lblNewLabel);

		text_field = new JTextField();
		search_panel.add(text_field);
		text_field.setColumns(20);

		JButton btn_search = new JButton("Search");
		btn_search.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				search();
			}
		});
		search_panel.add(btn_search);
		
		JButton clear_btn = new JButton("Clear");
		search_panel.add(clear_btn);
		clear_btn.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				text_field.setText("");
				refresh();
			}
		});
	}

	private void add_product()
	{
		ProductManager p = new ProductManager(null, false, false);
		getDesktopPane().add(p);
		Point current_win = getLocationOnScreen();
		int x = (int) current_win.getX() + 20;
		int y = (int) current_win.getY() + 5;
		p.setLocation(x, y);
		p.setVisible(true);
	}

	private void edit()
	{
		int row_index = product_table.getSelectedRow();
		if (row_index != -1)
		{
			if( product_table.getModel().getValueAt(row_index, 0) != null)
			{
				ProductManager p = new ProductManager(
						String.valueOf((Integer) product_table.getModel()
								.getValueAt(row_index, 0)), false, false);
				getDesktopPane().add(p);
				Point current_win = getLocationOnScreen();
				int x = (int) current_win.getX() + 20;
				int y = (int) current_win.getY() + 5;
				p.setLocation(x, y);
				p.setVisible(true);
			}
		}
	}
	
	private void complete()
	{
		int row_index = product_table.getSelectedRow();
		if (row_index != -1)
		{
			if( product_table.getModel().getValueAt(row_index, 0) != null)
			{
				ProductManager p = new ProductManager(
						String.valueOf(product_table.getModel().getValueAt(row_index, 0)), false, true);
				getDesktopPane().add(p);
				Point current_win = getLocationOnScreen();
				int x = (int) current_win.getX() + 20;
				int y = (int) current_win.getY() + 5;
				p.setLocation(x, y);
				p.setVisible(true);
			}
		}
	}

	private void details()
	{
		int row_index = product_table.getSelectedRow();
		if (row_index != -1)
		{
			if( product_table.getModel().getValueAt(row_index, 0) != null)
			{
				ProductManager p = new ProductManager(
						String.valueOf(product_table.getModel().getValueAt(row_index, 0)), true, false);
				getDesktopPane().add(p);
				Point current_win = getLocationOnScreen();
				int x = (int) current_win.getX() + 20;
				int y = (int) current_win.getY() + 5;
				p.setLocation(x, y);
				p.setVisible(true);
			}
		}
	}

	private void delete()
	{
		int row_index = product_table.getSelectedRow();
		if (row_index != -1)
		{
			if( product_table.getModel().getValueAt(row_index, 0) != null)
			{
				if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(this,
						"Are you sure you want to delete this product?",
						"Delete product", JOptionPane.YES_NO_OPTION))
				{
					stock_ctr.delete_product(String.valueOf(product_table.getModel().getValueAt(row_index, 0)));
					refresh();
				}
			}
		}

	}

	private void search()
	{
		Object[][] filling = new Object[1][6];
		String contents = text_field.getText();

		if (btn_pressed == 1)
		{
			if (is_numeric(contents))
			{
				filling[0] = stock_ctr.find_product_by_art_nr(contents, false, false);
			}
			else
			{
				filling[0] = stock_ctr.find_product_by_name(contents, false, false);
			}
		}
		else if (btn_pressed == 2)
		{
			if (is_numeric(contents))
			{
				filling[0] = stock_ctr.find_product_by_art_nr(contents, true, false);
			}
			else
			{
				filling[0] = stock_ctr.find_product_by_name(contents, true, false);
			}
		}
		else
		{
			if (is_numeric(contents))
			{
				filling[0] = stock_ctr.find_product_by_art_nr(contents, false, true);
			}
			else
			{
				filling[0] = stock_ctr.find_product_by_name(contents, false, true);
			}
		}
		product_table.setModel(new MyTableModel(filling, column_names));

	}

	private void fill_frame()
	{
		setBounds(140, 100, 680, 436);
		getContentPane().setLayout(new BorderLayout(0, 0));

		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.NORTH);
		panel.setLayout(new BorderLayout(0, 0));

		make_radio_buttons(panel);
		make_search(panel);
		make_ctr_buttons();
		make_table();
	}

	private boolean is_numeric(String checker)
	{
		try
		{
			int i = Integer.parseInt(checker);
		}
		catch (NumberFormatException nfe)
		{
			return false;
		}
		return true;
	}

}
